﻿using System;
using System.Text;
using System.Collections;
using System.Xml;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.Threading;

using Telerik.WebControls;
using UFSoft.UBF.UI.WebControls;
using UFSoft.UBF.UI.Controls;
using UFSoft.UBF.Util.Log;
using UFSoft.UBF.Util.Globalization;

using UFSoft.UBF.UI.IView;
using UFSoft.UBF.UI.Engine;
using UFSoft.UBF.UI.MD.Runtime;
using UFSoft.UBF.UI.ActionProcess;
using UFSoft.UBF.UI.WebControls.ClientCallBack;
using UFIDA.U9.Base.Profile.Proxy;
using UFIDA.U9.Base.Profile;
using UFSoft.UBF.UI.ControlModel;
using UFIDA.U9.UI.PDHelper;
using UFIDA.U9.Cust.SH.SHFastShipBP.Proxy;
using UFSoft.UBF.UI.WebControls.Association;
using UFSoft.UBF.UI.WebControls.Association.Adapter;
using System.Collections.Generic;
using UFIDA.U9.Cust.SH.SHFastShipBP;
using System.Collections.Specialized;
using UFSoft.UBF.Business;
using UFSoft.UBF.Util.DataAccess;
using UFIDA.U9.Cust.SH.SHFastShipSV.Proxy;
//using U9.VOB.Cus.HBHSH.CusShipBP.Proxy;
//using U9.VOB.Cus.HBHSH.CusShipBP;
using UFIDA.U9.Cust.SH.SHFastShipUI.Pub;
using UFSoft.UBF.UI.WebControlAdapter;
using UFIDA.U9.Cust.SH.SHFastShipBE;
using UFSoft.UBF.ExportService;



/***********************************************************************************************
 * Form ID: 
 * UIFactory Auto Generator 
 ***********************************************************************************************/
namespace SHFastShipUI
{
    public partial class SHFastShipMainFormWebPart
    {
        #region Custome eventBind

        SHFastShipRecord ShipDoc { get { return this.Model.SHFastShip.FocusedRecord; } }
        IUIRecordCollection ShipDocLines { get { return this.Model.SHFastShip_SHFastShipLine.Records; } }


        private void IsZT68_TextChanged_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();

            if (this.Model.SHFastShip.FocusedRecord != null)
            {
                if (this.Model.SHFastShip.FocusedRecord.IsZT == true)
                {
                    //this.Model.SHFastShip.FocusedRecord.Agency = null;
                    //this.Model.SHFastShip.FocusedRecord.Agency_Code = "";
                    //this.Model.SHFastShip.FocusedRecord.Agency_Name = "";

                    this.Model.SHFastShip.FocusedRecord.GeneralAgency = null;
                    this.Model.SHFastShip.FocusedRecord.GeneralAgency_Code = "";
                    this.Model.SHFastShip.FocusedRecord.GeneralAgency_Name = "";
                }
            }

            IsZT68_TextChanged_DefaultImpl(sender, e);
        }

        //整单打开
        private void Menu6_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();

            if (ShipDoc != null && ShipDoc.ID > 0)
            {
                DocOpenProxy proxy = new DocOpenProxy();
                proxy.FastShipID = ShipDoc.ID;
                proxy.Do();
            }
            this.Action.NavigateAction.Refresh(null);

            Menu6_Click_DefaultImpl(sender, e);
        }

        //取消预留
        private void Menu7_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();

            ShipDoc.Reserved = false;
            this.LoadAvailableQty(true);
            this.Action.CommonAction.Save();

            Menu7_Click_DefaultImpl(sender, e);
        }

        //整单关闭
        private void Menu1_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();

            if (this.Model.SHFastShip.FocusedRecord != null && this.Model.SHFastShip.FocusedRecord.ID > 0)
            {
                DocCloseProxy proxy = new DocCloseProxy();
                proxy.FastShipID = this.Model.SHFastShip.FocusedRecord.ID;
                proxy.Do();
            }
            this.Action.NavigateAction.Refresh(null);

            Menu1_Click_DefaultImpl(sender, e);
        }

        private void OnReserve_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();

            //刷新可用量
            if (this.Model.SHFastShip.IsDataChanged || this.Model.SHFastShip_SHFastShipLine.IsDataChanged)
                throw new Exception("单据尚未保存，请先保存单据!");
            this.LoadAvailableQty(true);

            //预留
            this.ReserveQty();
            this.Action.CommonAction.Save();

            OnReserve_Click_DefaultImpl(sender, e);
        }

        private void OnLoadAvailableQty_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();

            if (this.Model.SHFastShip.IsDataChanged || this.Model.SHFastShip_SHFastShipLine.IsDataChanged)
                throw new Exception("单据尚未保存，请先保存单据!");

            this.LoadAvailableQty(true);
            this.Action.CommonAction.Save();

            OnLoadAvailableQty_Click_DefaultImpl(sender, e);
        }

        private void ReserveQty()
        {
            int docstate = 3;

            if (ShipDoc != null)
            {
                ShipDoc.Reserved = true;
            }

            foreach (SHFastShip_SHFastShipLineRecord record in this.Model.SHFastShip_SHFastShipLine.Records)
            {
                if (record.Qty > record.AvailableQty)
                    record.Qty = record.AvailableQty;
                record.AvailableQty -= record.Qty;

                //设置行状态关闭
                if (record.AvailableQty == 0)
                {
                    //record.DocLineState = 3;
                }
                else
                {
                    docstate = 0;
                }

            }

            //设置整单状态关闭
            if (this.Model.SHFastShip.FocusedRecord != null)
            {
                this.Model.SHFastShip.FocusedRecord.DocState = docstate;
                if (docstate == 3)
                {
                    foreach (SHFastShip_SHFastShipLineRecord record in ShipDocLines)
                    {
                        record.DocLineState = 3;
                    }
                }
            }

        }

        private void LoadAvailableQty(bool IncludeDoc)
        {
            List<ItemQryDTOData> list = new List<ItemQryDTOData>();
            Dictionary<long, ItemQryDTOData> indict = new Dictionary<long, ItemQryDTOData>();

            foreach (SHFastShip_SHFastShipLineRecord record in this.Model.SHFastShip_SHFastShipLine.Records)
            {
                ItemQryDTOData dto = new ItemQryDTOData();
                dto.ItemID = record.Item;
                dto.WH = record.WH;
                dto.LotID = record.LotMaster ?? 0;
                if (!indict.ContainsKey(dto.ItemID))
                {
                    indict.Add(dto.ItemID, dto);
                    list.Add(dto);
                }
            }

            if (list.Count > 0)
            {
                //调用BP计算可用量
                GetAvailableQtyProxy proxy = new GetAvailableQtyProxy();
                proxy.ItemList = list;
                List<ItemInfoDTOData> retlist = proxy.Do();

                //刷新界面
                if (retlist.Count <= 0) return;
                Dictionary<long, ItemInfoDTOData> itemdict = new Dictionary<long, ItemInfoDTOData>();

                foreach (ItemInfoDTOData dto in retlist)
                {
                    itemdict.Add(dto.ItemID, dto);
                }

                foreach (SHFastShip_SHFastShipLineRecord record in this.Model.SHFastShip_SHFastShipLine.Records)
                {
                    //库存数量
                    record.StoreQty = itemdict[record.Item].StoreQty;
                    //可用数量
                    if (IncludeDoc)
                    {
                        record.AvailableQty = itemdict[record.Item].AvailableQty + record.Qty;
                    }
                    else
                    {
                        record.AvailableQty = itemdict[record.Item].AvailableQty;
                    }
                    if (record.AvailableQty > record.StoreQty)
                        record.AvailableQty = record.StoreQty;

                    //缺货数量
                    if (record.OrderQty > record.AvailableQty)
                        record.LackQty = record.OrderQty - record.AvailableQty;
                    else
                        record.LackQty = 0;

                    //预留后，可用量排出本单
                    if (ShipDoc.Reserved == true)
                    {
                        record.AvailableQty -= record.Qty;
                        if (record.AvailableQty > record.StoreQty)
                            record.AvailableQty = record.StoreQty;
                    }
                }
            }
        }

        private void OnPrintSHBill_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();

            //2011-11-5日添加判断单据是否审核，如未审则不能打印


            //IExportSettings settings = ExportServiceFactory.GetInstance().CreateExportSettingsObject();
            //settings.PrintTemplateCatalogType = "UFIDA.U9.CustPrint";
            //settings.UserDataCallBack = new DataCallBackHandle(this.GetPrintData);
            //UIActionEventArgs ex = new UIActionEventArgs();
            //ex.Tag = settings;
            //UFIDA.U9.UI.Commands.CommandFactory.DoCommand("OnPrint", (BaseAction)ShipDocPart.Action, sender, ex);


            OnPrintSHBill_Click_DefaultImpl(sender, e);
        }



        private void OnPrintSHShip_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();



            OnPrintSHShip_Click_DefaultImpl(sender, e);
        }

        private void IsZY17_TextChanged_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();

            if (this.Model.SHFastShip.FocusedRecord == null) return;
            if (this.Model.SHFastShip.FocusedRecord.Customer <= 0) throw new Exception("必须先录入客户！");

            //ZY=true时，获取默认直运仓库
            if (this.Model.SHFastShip.FocusedRecord.IsZY == true)
            {
                GetZYWHProxy proxy = new GetZYWHProxy();
                proxy.CustID = this.Model.SHFastShip.FocusedRecord.Customer;
                EntityDTOData dto = proxy.Do();
                if (dto != null)
                {
                    this.Model.SHFastShip.FocusedRecord.SaleWH = dto.ID;
                    this.Model.SHFastShip.FocusedRecord.SaleWH_Code = dto.Code;
                    this.Model.SHFastShip.FocusedRecord.SaleWH_Name = dto.Name;
                }
            }
            else
            {
                this.Model.SHFastShip.FocusedRecord.SaleWH = null;
                this.Model.SHFastShip.FocusedRecord.SaleWH_Code = "";
                this.Model.SHFastShip.FocusedRecord.SaleWH_Name = "";
            }

            IsZY17_TextChanged_DefaultImpl(sender, e);
        }

        private void Customer0_TextChanged_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();

            if (this.Model.SHFastShip.FocusedRecord == null) return;

            //依据客户获取默认仓库
            GetDefaultWHProxy proxy = new GetDefaultWHProxy();
            proxy.CustID = this.Model.SHFastShip.FocusedRecord.Customer;
            EntityDTOData dto = proxy.Do();
            if (dto != null)
            {
                this.Model.SHFastShip.FocusedRecord.SaleWH = dto.ID;
                this.Model.SHFastShip.FocusedRecord.SaleWH_Code = dto.Code;
                this.Model.SHFastShip.FocusedRecord.SaleWH_Name = dto.Name;
            }

            Customer0_TextChanged_DefaultImpl(sender, e);
        }

        private void OnShipToRCV_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();

            if ((this.Model.SHFastShip.FocusedRecord.ShipDoc ?? 0) <= 0)
                throw new Exception("没有生成销售出货单,请先审核!");


            //List<CusShipDTOData> list = new List<CusShipDTOData>();
            //CusShipDTOData dto = new CusShipDTOData();
            //dto.ID = this.Model.SHFastShip.FocusedRecord.ShipDoc ?? 0;
            //list.Add(dto);

            //CusShipBPProxy proxy = new CusShipBPProxy();
            //proxy.CusShipDTO = list;
            //List<CusShipRtnDTOData> rtnList = proxy.Do();
            //if (rtnList != null && rtnList.Count > 0)
            //    if (rtnList[0].IsSuccess == false)
            //        throw new Exception(rtnList[0].ErrorInfo);

            OnShipToRCV_Click_DefaultImpl(sender, e);
        }

        private void OnShipToSO_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();

            if ((this.Model.SHFastShip.FocusedRecord.ShipDoc ?? 0) <= 0)
                throw new Exception("没有生成销售出货单,请先审核!");


            //List<CusShipDTOData> list = new List<CusShipDTOData>();
            //CusShipDTOData dto = new CusShipDTOData();
            //dto.ID = this.Model.SHFastShip.FocusedRecord.ShipDoc ?? 0;
            //list.Add(dto);

            //CusShipLineToSoBPProxy proxy = new CusShipLineToSoBPProxy();
            //proxy.CusShipDTO = list;
            //List<CusShipRtnDTOData> rtnList = proxy.Do();
            //if (rtnList != null && rtnList.Count > 0)
            //    if (rtnList[0].IsSuccess == false)
            //        throw new Exception(rtnList[0].ErrorInfo);

            OnShipToSO_Click_DefaultImpl(sender, e);
        }

        private void ReLoad_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();
            if (this.CurrentState["SHSOLineInfos"] != null)
            {
                List<ItemInfoDTOData> dtos = this.CurrentState["SHSOLineInfos"] as List<ItemInfoDTOData>;
                this.CurrentState["SHSOLineInfos"] = null;

                foreach (ItemInfoDTOData dto in dtos)
                {
                    if (this.Model.SHFastShip_SHFastShipLine.Records.FindValue("SOLine", dto.SOLineID) != null)
                        continue;
                    SHFastShip_SHFastShipLineRecord newlineRecord = this.Model.SHFastShip_SHFastShipLine.AddNewUIRecord();
                    SetItemInfo(newlineRecord, dto);
                    newlineRecord.SetParentRecord(this.Model.SHFastShip.FocusedRecord);
                }
            }

            ReLoad_Click_DefaultImpl(sender, e);
        }

        private void OnSOList_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();

            //this.ShowModalDialog("09f8a2fb-ac8e-4140-8dd6-c4704b7b4075", "销售订单列表", "992", "504");
            if (this.Model.SHFastShip.FocusedRecord == null || this.Model.SHFastShip.FocusedRecord.Customer <= 0)
                throw new Exception("请先选择客户!");


            string iszy = GetCustomerZY(this.Model.SHFastShip.FocusedRecord.Customer_Code);

            NameValueCollection param = new NameValueCollection();
            param.Add("BtnReloadName", this.ReLoad.ClientID.Replace("_", "$"));
            param.Add("CustID", this.Model.SHFastShip.FocusedRecord.Customer.ToString());
            param.Add("CustCode", this.Model.SHFastShip.FocusedRecord.Customer_Code);
            param.Add("ISZY", iszy);
            string whid = "0";
            if (this.Model.SHFastShip.FocusedRecord.WH != null) this.Model.SHFastShip.FocusedRecord.WH.ToString();
            param.Add("WHID", whid);
            this.ShowAtlasModalDialog(this.ReLoad, "09f8a2fb-ac8e-4140-8dd6-c4704b7b4075", "销售订单列表", "992", "504", null, param, true, true, false);

            OnSOList_Click_DefaultImpl(sender, e);
        }

        private string GetCustomerZY(string cust)
        {
            //select code,org,DescFlexField_PrivateDescSeg8 from CBO_Customer
            //    where Code='01.01.01.01' and Org=1001106046291924
            //获取是否下线客户
            string sql = "select DescFlexField_PrivateDescSeg8 from CBO_Customer";
            sql += " where 1=1";
            sql += " and Org=" + PDContext.Current.OrgRef.ID;
            sql += " and Code='" + cust + "'";
            object iszy = "";

            DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out iszy);

            return iszy.ToString();
        }



        //BtnSave_Click...
        private void BtnSave_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();

            //设置单号,设置单号改在新增保存时
            //SetDocNo();

            //刷新信用额度
            SetCredit();

            BtnSave_Click_DefaultImpl(sender, e);

        }

        private void SetCredit()
        {
            if (this.Model.SHFastShip.FocusedRecord.Customer <= 0) throw new Exception("先录入客户！");
            GetCustomerCreditSVProxy proxy = new GetCustomerCreditSVProxy();
            proxy.CustomerID = this.Model.SHFastShip.FocusedRecord.Customer;
            //组织编码 1000 双虎组织
            //组织编码 20XX 总经销
            //组织编码 21XX 办事处
            long orgid;
            if (PDContext.Current.OrgRef.CodeColumn == "1000" || PDContext.Current.OrgRef.CodeColumn.StartsWith("21"))
            {
                //取 双虎组织 信用余额
                orgid = 1001106046291924;
            }
            else if (PDContext.Current.OrgRef.CodeColumn.StartsWith("20"))
            {
                //总经销 取 自身的信用
                orgid = PDContext.Current.OrgRef.ID;
            }
            else
                throw new Exception("组织编码设置错误，无法获取信用！");

            decimal credit = proxy.Do(orgid);
            this.Model.SHFastShip.FocusedRecord.Credit = credit;
        }

        private void SetDocNo()
        {
            if (string.IsNullOrEmpty(this.Model.SHFastShip.FocusedRecord.DocNo))
            {
                GetDocNoProxy proxy = new GetDocNoProxy();
                proxy.Prefix = "FH";
                proxy.Prefix += PDContext.Current.LoginDate.Year.ToString().Substring(2);
                proxy.Prefix += PDContext.Current.LoginDate.Month.ToString().PadLeft(2, '0');
                string docno = proxy.Do();
                this.Model.SHFastShip.FocusedRecord.DocNo = docno;
            }
        }

        //BtnCancel_Click...
        private void BtnCancel_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.


            BtnCancel_Click_DefaultImpl(sender, e);
        }

        //BtnAdd_Click...
        private void BtnAdd_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();

            BtnAdd_Click_DefaultImpl(sender, e);
        }

        //BtnDelete_Click...
        private void BtnDelete_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();

            BtnDelete_Click_DefaultImpl(sender, e);
        }

        //BtnCopy_Click...
        private void BtnCopy_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();
            if (this.Model.SHFastShip.IsDataChanged || this.Model.SHFastShip_SHFastShipLine.IsDataChanged)
                throw new Exception("单据尚未保存,不能复制!");

            BtnCopy_Click_DefaultImpl(sender, e);

            //数据修正
            this.AfterCopy();
            this.LoadAvailableQty(false);
        }

        private void AfterCopy()
        {
            SHFastShipRecord doc = this.Model.SHFastShip.FocusedRecord;
            if (doc == null) return;
            doc.DocNo = "";
            doc.DocDate = DateTime.Now;
            doc.Credit = null;
            doc.DocState = 0;
            //doc.Memo = "";

            for (int i = this.Model.SHFastShip_SHFastShipLine.Records.Count - 1; i >= 0; i--)
            {
                SHFastShip_SHFastShipLineRecord line = this.Model.SHFastShip_SHFastShipLine.Records[i] as SHFastShip_SHFastShipLineRecord;

                if (line.SOLine != null && line.SOLine > 0)
                {
                    //有来源单据的不能复制
                    line.Delete();
                }
                else
                {
                    line.StoreQty = 0;
                    line.AvailableQty = 0;
                    line.Price = 0;
                    line.Amount = 0;
                }
            }
        }

        //BtnSubmit_Click...
        private void BtnSubmit_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.


            BtnSubmit_Click_DefaultImpl(sender, e);
        }

        //BtnApprove_Click...
        private void BtnApprove_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();

            if (this.Model.SHFastShip.IsDataChanged) throw new Exception("单据已修改，请先保存！");
            if (this.Model.SHFastShip_SHFastShipLine.IsDataChanged) throw new Exception("单据已修改，请先保存！");

            if (this.Model.SHFastShip.FocusedRecord != null && this.Model.SHFastShip.FocusedRecord.ID > 0)
            {
                ApproveBPProxy proxy = new ApproveBPProxy();
                proxy.DocID = this.Model.SHFastShip.FocusedRecord.ID;
                proxy.Do();
                this.Action.NavigateAction.Refresh(null);
            }
            else
                throw new Exception("请先选择单据！");

            BtnApprove_Click_DefaultImpl(sender, e);
        }

        //BtnUndoApprove_Click...
        private void BtnUndoApprove_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.
            this.Model.ClearErrorMessage();

            if (this.Model.SHFastShip.FocusedRecord != null && this.Model.SHFastShip.FocusedRecord.ID > 0)
            {
                UnApproveBPProxy proxy = new UnApproveBPProxy();
                proxy.DocID = this.Model.SHFastShip.FocusedRecord.ID;
                proxy.Do();
                this.Action.NavigateAction.Refresh(null);
            }
            else
                throw new Exception("请先选择单据！");

            BtnUndoApprove_Click_DefaultImpl(sender, e);
        }

        //BtnFind_Click...
        private void BtnFind_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.


            BtnFind_Click_DefaultImpl(sender, e);
        }

        //BtnList_Click...
        private void BtnList_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.


            BtnList_Click_DefaultImpl(sender, e);
            //列表导航
            NavigateManager.NavigatePage(this, "UFIDA.U9.Cust.SH.SHFastShipList");

            //this.Action.CommonAction.CurrentPart.NavigatePage("UFIDA.U9.Cust.SH.SHFastShipList", null);
        }

        //BtnFirstPage_Click...
        private void BtnFirstPage_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.


            BtnFirstPage_Click_DefaultImpl(sender, e);
        }

        //BtnPrevPage_Click...
        private void BtnPrevPage_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.


            BtnPrevPage_Click_DefaultImpl(sender, e);
        }

        //BtnNextPage_Click...
        private void BtnNextPage_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.


            BtnNextPage_Click_DefaultImpl(sender, e);
        }

        //BtnLastPage_Click...
        private void BtnLastPage_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.


            BtnLastPage_Click_DefaultImpl(sender, e);
        }

        //BtnAttachment_Click...
        private void BtnAttachment_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.


            BtnAttachment_Click_DefaultImpl(sender, e);
        }

        //BtnFlow_Click...
        private void BtnFlow_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.


            BtnFlow_Click_DefaultImpl(sender, e);
        }

        //BtnOutput_Click...
        private void BtnOutput_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.


            BtnOutput_Click_DefaultImpl(sender, e);
        }

        //BtnPrint_Click...
        private void BtnPrint_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.


            BtnPrint_Click_DefaultImpl(sender, e);
        }

        //BtnOk_Click...
        private void BtnOk_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.


            BtnOk_Click_DefaultImpl(sender, e);
        }

        //BtnClose_Click...
        private void BtnClose_Click_Extend(object sender, EventArgs e)
        {
            //调用模版提供的默认实现.--默认实现可能会调用相应的Action.


            BtnClose_Click_DefaultImpl(sender, e);
        }






        #region 自定义数据初始化加载和数据收集
        private void OnLoadData_Extend(object sender)
        {
            OnLoadData_DefaultImpl(sender);
        }
        private void OnDataCollect_Extend(object sender)
        {
            OnDataCollect_DefaultImpl(sender);
        }
        #endregion

        #region 自己扩展 Extended Event handler
        public void AfterOnLoad()
        {
            SetPriceTypeName();
        }

        public void AfterCreateChildControls()
        {
            //InitGridLineNo();
            //注册查找
            RegisterFindRef();

            //注册删除确认窗口
            PDFormMessage.ShowDelConfirmDialog(this.Page, PDResource.GetDeleteConfirmInfo(), "", this.BtnDelete);

            //设置Grid料品多选参照Postback
            RegisterItemMultiSelectRefPostback();

            //启用个性化
            UFIDA.U9.UI.PDHelper.PersonalizationHelper.SetPersonalizationEnable(this, true);

            //绑定小数位
            BindRoundField();

        }

        /// <summary>
        /// 绑定界面的小数位数
        /// </summary>
        private void BindRoundField()
        {
            UFWebNumberColumnWrapper wrapper = null;

            wrapper = this.DataGrid5.Columns["Amount"] as UFWebNumberColumnWrapper;
            if (wrapper == null) return;
            wrapper.DecimalDigits = 4;

            wrapper = this.DataGrid5.Columns["FinalAmount"] as UFWebNumberColumnWrapper;
            if (wrapper == null) return;
            wrapper.DecimalDigits = 4;

            wrapper = this.DataGrid5.Columns["TBFee"] as UFWebNumberColumnWrapper;
            if (wrapper == null) return;
            wrapper.DecimalDigits = 4;

            wrapper = this.DataGrid5.Columns["ZMDProfit"] as UFWebNumberColumnWrapper;
            if (wrapper == null) return;
            wrapper.DecimalDigits = 4;

            wrapper = this.DataGrid5.Columns["ZDLProfit"] as UFWebNumberColumnWrapper;
            if (wrapper == null) return;
            wrapper.DecimalDigits = 4;

            wrapper = this.DataGrid5.Columns["GSTransportFee"] as UFWebNumberColumnWrapper;
            if (wrapper == null) return;
            wrapper.DecimalDigits = 4;

            wrapper = this.DataGrid5.Columns["GSProfit"] as UFWebNumberColumnWrapper;
            if (wrapper == null) return;
            wrapper.DecimalDigits = 4;

            wrapper = this.DataGrid5.Columns["DevideRatio"] as UFWebNumberColumnWrapper;
            if (wrapper == null) return;
            wrapper.DecimalDigits = 4;
        }


        /// <summary>
        /// 设置料品多选参照
        /// </summary>
        private void RegisterItemMultiSelectRefPostback()
        {
            AssociationControl assocControl = new AssociationControl();
            assocControl.SourceServerControl = this.DataGrid5;
            assocControl.SourceControl.EventName = "OnCellDataChanged";

            ((IUFClientAssoGrid)assocControl.SourceControl).FireEventCols.Add("Item");
            ((IUFClientAssoGrid)assocControl.SourceControl).FireEventCols.Add("WH");

            CodeBlock gridCB = new CodeBlock();
            UFWebClientGridAdapter gridAdapter = new UFWebClientGridAdapter(this.DataGrid5);
            gridAdapter.IsPostBack = true;
            gridAdapter.PostBackTag = "Item_OnCellDataChanged";
            gridCB.TargetControls.addControl(gridAdapter);
            assocControl.addBlock(gridCB);

            UFGrid detailGrid = this.DataGrid5 as UFGrid;
            detailGrid.GridCustomerPostBackEvent += new GridCustomerPostBackDelegate(ItemMultiSelectRefPostBack);
        }

        void ItemMultiSelectRefPostBack(object sender, GridCustomerPostBackEventArgs e)
        {
            if (e.PostTag.ToString() != "Item_OnCellDataChanged") return;
            if (e.ColIndex == -1) return;
            DoDataCollect();

            //SHFastShip_SHFastShipLineRecord lineRecord = this.Model.SHFastShip_SHFastShipLine.FocusedRecord;
            //if (lineRecord == null) return;

            if (this.DataGrid5.Columns[e.ColIndex].UIFieldID == "Item")
                SetItemMultiSelectRef();

            if (this.DataGrid5.Columns[e.ColIndex].UIFieldID == "WH")
                SetWHRef();

        }

        private void SetWHRef()
        {
            SHFastShip_SHFastShipLineRecord record = this.Model.SHFastShip_SHFastShipLine.FocusedRecord;
            if (record.Item <= 0) return;

            List<ItemQryDTOData> list = new List<ItemQryDTOData>();
            ItemQryDTOData dto = new ItemQryDTOData();
            dto.ItemID = record.Item;
            dto.LotID = record.LotMaster ?? 0;
            dto.WH = record.WH;
            list.Add(dto);

            GetAvailableQtyProxy proxy = new GetAvailableQtyProxy();
            proxy.ItemList = list;
            List<ItemInfoDTOData> retlist = proxy.Do();
            if (retlist.Count <= 0) return;
            record.StoreQty = retlist[0].StoreQty;
            record.AvailableQty = retlist[0].AvailableQty;
        }

        private void SetItemMultiSelectRef()
        {
            List<Dictionary<string, string>> itemSelectedRefRecords = null;

            if (this.CurrentState["CustSHItemRef"] == null)
            {
                //手工录入料号
                SHFastShip_SHFastShipLineRecord record = this.Model.SHFastShip_SHFastShipLine.FocusedRecord;
                if (record.Item > 0)
                {
                    Dictionary<string, string> selectedDict = new Dictionary<string, string>();

                    selectedDict["ID"] = record.Item.ToString();
                    selectedDict["Code"] = record.Item_Code.ToString();
                    selectedDict["Name"] = record.Item_Name.ToString();

                    itemSelectedRefRecords = new List<Dictionary<string, string>>();
                    itemSelectedRefRecords.Add(selectedDict);
                }
            }
            else
            {
                //参照确定后返回
                itemSelectedRefRecords = this.CurrentState["CustSHItemRef"] as List<Dictionary<string, string>>;
                this.CurrentState["CustSHItemRef"] = null;

            }

            if (itemSelectedRefRecords == null || itemSelectedRefRecords.Count == 0) return;

            List<ItemInfoDTOData> ItemInfoList = this.GetShipItemInfo(itemSelectedRefRecords);
            if (ItemInfoList == null || ItemInfoList.Count == 0)
                return;

            if (this.Model.SHFastShip_SHFastShipLine.FocusedRecord != null)
            {
                SetItemInfo(this.Model.SHFastShip_SHFastShipLine.FocusedRecord, ItemInfoList[0]);
            }

            for (int i = 1; i < ItemInfoList.Count; i++)
            {
                //行上已经有同一个ID的Item，则不在增加
                if (this.Model.SHFastShip_SHFastShipLine.Records.FindValue("Item", ItemInfoList[i].ItemID) != null)
                    continue;
                SHFastShip_SHFastShipLineRecord newlineRecord = this.Model.SHFastShip_SHFastShipLine.AddNewUIRecord();
                SetItemInfo(newlineRecord, ItemInfoList[i]);
                newlineRecord.SetParentRecord(this.Model.SHFastShip.FocusedRecord);
            }
        }

        private void SetItemInfo(SHFastShip_SHFastShipLineRecord record, ItemInfoDTOData iteminfo)
        {
            if (record.DocLineNo == 0)
                record.DocLineNo = this.GetMaxLineNo() + 10;

            record.DocLineState = 0;
            record.Item = iteminfo.ItemID;
            record.Item_Code = iteminfo.ItemCode;
            record.Item_Name = iteminfo.ItemName;
            record.Item_SPECS = iteminfo.ItemSpec;

            record.StoreQty = iteminfo.StoreQty;
            record.AvailableQty = iteminfo.AvailableQty;
            record.Price = iteminfo.Price;
            record.Qty = iteminfo.Qty;
            record.Weight = iteminfo.Weight;
            record.Amount = record.Price * record.Qty;

            record.WH = iteminfo.WH;
            record.WH_Code = iteminfo.WHCode;
            record.WH_Name = iteminfo.WHName;

            record.UOM = iteminfo.UOM;
            record.UOM_Code = iteminfo.UOMCode;
            record.UOM_Name = iteminfo.UOMName;

            if (iteminfo.PriceType != null)
            {
                record.PriceType = iteminfo.PriceType;
                if (iteminfo.PriceType.Trim() == "0")
                    record.PriceTypeName = "正价";
                else if (iteminfo.PriceType.Trim() == "1")
                    record.PriceTypeName = "特价";
            }

            if (iteminfo.SOLineID > 0)
            {
                record.SODocNo = iteminfo.SODocNo;
                record.SOLine = iteminfo.SOLineID;
                record.ProductPrice = iteminfo.ProductPrice;
                record.MarkPrice = iteminfo.MarkPrice;
                record.TransportPrice = iteminfo.TransportPrice;
                record.StorePrice = iteminfo.StorePrice;
                record.Price = iteminfo.Price;
                //门店新增字段
                record.Deposit = iteminfo.Deposit;
                record.MDLineID = iteminfo.MDLineID;
                record.MDItemCode = iteminfo.MDItemCode;
                record.SalePrice = iteminfo.SalePrice;
                record.OrderQty = iteminfo.OrderQty;
                record.MDDocNo = iteminfo.MDDocNo;

                if (this.Model.SHFastShip.FocusedRecord != null)
                {
                    //门店新增字段-门店订单说明
                    if (string.IsNullOrEmpty(this.Model.SHFastShip.FocusedRecord.MDMemo)
                        && !string.IsNullOrEmpty(iteminfo.MDMemo))
                        this.Model.SHFastShip.FocusedRecord.MDMemo = iteminfo.MDMemo;
                    //门店新增字段-门店订单号
                    //if (string.IsNullOrEmpty(this.Model.SHFastShip.FocusedRecord.MDDocNo)
                    //    && !string.IsNullOrEmpty(iteminfo.MDDocNo))
                    //    this.Model.SHFastShip.FocusedRecord.MDDocNo = iteminfo.MDDocNo;

                    //导入SO备注
                    if (string.IsNullOrEmpty(this.Model.SHFastShip.FocusedRecord.Memo)
                        && !string.IsNullOrEmpty(iteminfo.Memo))
                        this.Model.SHFastShip.FocusedRecord.Memo = iteminfo.Memo;
                }
            }
            //沙发处理
            if (!string.IsNullOrEmpty(iteminfo.Lot))
            {
                record.SFSpec = iteminfo.SFSpec;
                record.SFMaterial = iteminfo.SFMaterial;
                record.Direction = iteminfo.Direction;
                record.SFCount = iteminfo.SFCount;
                record.LastCustomer = iteminfo.LastCustomer;
                record.Lot = iteminfo.Lot;
                record.LotMaster = iteminfo.LotID;
            }
        }

        private int GetMaxLineNo()
        {
            int lineno = 0;
            foreach (SHFastShip_SHFastShipLineRecord record in this.Model.SHFastShip_SHFastShipLine.Records)
            {
                lineno = (lineno >= record.DocLineNo ? lineno : record.DocLineNo);
            }
            return lineno;
        }

        private List<ItemInfoDTOData> GetShipItemInfo(List<Dictionary<string, string>> itemSelectedRefRecords)
        {
            List<ItemQryDTOData> list = new List<ItemQryDTOData>();
            foreach (Dictionary<string, string> item in itemSelectedRefRecords)
            {
                ItemQryDTOData dto = new ItemQryDTOData();
                dto.ItemID = PubTool.ToLong(item["ID"].ToString());
                if (this.Model.SHFastShip.FocusedRecord.Customer <= 0)
                    throw new Exception("客户为空，必须录入！");
                dto.CustID = this.Model.SHFastShip.FocusedRecord.Customer;
                if (this.Model.SHFastShip.FocusedRecord.WH != null && this.Model.SHFastShip.FocusedRecord.WH > 0)
                    dto.WH = this.Model.SHFastShip.FocusedRecord.WH ?? 0;
                else
                    dto.WH = 0;
                list.Add(dto);
            }

            GetItemInfoProxy proxy = new GetItemInfoProxy();
            proxy.ItemQryList = list;
            return proxy.Do();
        }

        /// <summary>
        /// 数据收集
        /// </summary>
        private void DoDataCollect()
        {
            OnDataCollect(this); //当前事件先执行数据收集
            this.IsDataBinding = true; //当前事件执行后会进行数据绑定
            this.IsConsuming = false;

            this.Model.ClearErrorMessage();
        }

        /// <summary>
        /// 注册查找参照
        /// </summary>
        private void RegisterFindRef()
        {
            PDFormMessage.ShowConfirmDialog(this.Page, "7a5b4871-7469-400c-ac17-07a72166c109", "580", "408", Title, wpFindID.ClientID, this.BtnFind);
        }


        #region Grid单据行号处理

        /// <summary>
        /// 设置Grid行号的起始值和步长
        /// </summary>
        private void InitGridLineNo()
        {
            GetProfileValueProxy bpObj = new GetProfileValueProxy();
            bpObj.ProfileCode = "SysLineNo";
            PVDTOData pVTDOData = bpObj.Do();

            int LineNoStep = Convert.ToInt32(pVTDOData.ProfileValue);
            //行GRID
            ((IAutoRowNo)this.DataGrid5.Columns[1]).Sequence = true;
            ((IAutoRowNo)this.DataGrid5.Columns[1]).SequenceStep = LineNoStep;
            ((IAutoRowNo)this.DataGrid5.Columns[1]).SequenceStart = LineNoStep;
        }

        #endregion

        public void AfterEventBind()
        {
        }

        public void BeforeUIModelBinding()
        {
            //SetDefaultValue();

            SetPriceTypeName();

            //单据类型参数赋值
            if (this.Model.SHFastShip.FocusedRecord != null
                && this.Model.SHFastShip.FocusedRecord.DataRecordState != DataRowState.Added)
            {
                this.Model.SHFastShip.FocusedRecord.ShipDocTypeParam = "ShipDocType";
            }
        }

        public void AfterUIModelBinding()
        {
            SetControlsStatus();
            //SetDefaultValue();
            //SetUIEditable();

            SetPriceTypeName();

            SetWaterMark();
        }

        #region 水印效果

        private void SetWaterMark()
        {
            SHFastShipRecord headRecord = this.Model.SHFastShip.FocusedRecord;
            if (headRecord == null) return;

            //if (headRecord.DocState == (int)FastShipDocStateData.DocOpen)
            //{
            //    UFWaterMark mark_Opened = new UFWaterMark();
            //    mark_Opened.TargetControl = this.DocState0;
            //    mark_Opened.WaterMarkType = enuMarkType.Open;
            //}

            if (headRecord.DocState == (int)FastShipDocStateData.DocAudited)
            {
                UFWaterMark mark_Approving = new UFWaterMark();
                mark_Approving.TargetControl = this.DocState0;
                mark_Approving.WaterMarkType = enuMarkType.Approved;
            }
        }

        #endregion

        private void SetPriceTypeName()
        {
            foreach (SHFastShip_SHFastShipLineRecord record in this.Model.SHFastShip_SHFastShipLine.Records)
            {
                if (record.PriceType == null) continue;

                if (record.PriceType.Trim() == "0")
                {
                    record.PriceTypeName = "正价";
                }
                else if (record.PriceType.Trim() == "1")
                {
                    record.PriceTypeName = "特价";
                }
            }
        }

        /// <summary>
        /// 依据状态设置界面是否可编辑
        /// </summary>
        private void SetUIEditable()
        {
            if (ShipDoc == null || (ShipDoc.DocState == 0&&ShipDoc.Reserved==false))
            {
                //新增、开立态
                if (ShipDoc.DocType_Code == "U914")
                {
                    //淘宝发货
                    SetTBFieldReadOnly(false);
                }
                else
                {
                    //标准发货
                    Card4.ReadOnly = false;
                }
                
            }
            else if (ShipDoc.DocState == 1 || ShipDoc.DocState == 2
                || ShipDoc.DocState == 3 || ShipDoc.Reserved==true)
            {
                //已审、挂起态、关闭、预留
                if (this.Model.SHFastShip.FocusedRecord.DocType_Code == "U914")
                {
                    //淘宝发货
                    SetTBFieldReadOnly(true);
                }
                else
                {
                    //标准发货
                    Card4.ReadOnly = true;
                }
            }

            if (ShipDoc != null && ShipDoc.DocType_Code == "U914")
            {
                //淘宝发货
                if (this.Model.SHFastShip.FocusedRecord.RebatesDate != null)
                {
                    SetRebatesFieldReadOnly(true);
                }
                else
                {
                    SetRebatesFieldReadOnly(false);
                }

                if (this.Model.SHFastShip.FocusedRecord.IsZT == true)
                {
                    (GeneralAgency54 as IUIFieldBindingDataBindControl).ReadOnly = true;
                }
            }

            //固定只读栏目
            (Org0 as IUIFieldBindingDataBindControl).ReadOnly = true;
            (DocNo0 as IUIFieldBindingDataBindControl).ReadOnly = true;
            (DocState0 as IUIFieldBindingDataBindControl).ReadOnly = true;
            (Credit44 as IUIFieldBindingDataBindControl).ReadOnly = true;
            (TBAmount50 as IUIFieldBindingDataBindControl).ReadOnly = true;
            (Reserved33 as IUIFieldBindingDataBindControl).ReadOnly = true;
        }

        private void SetRebatesFieldReadOnly(bool flag)
        {
            (TBDocNo56 as IUIFieldBindingDataBindControl).ReadOnly = flag;
            (FinalCustomer93 as IUIFieldBindingDataBindControl).ReadOnly = flag;
            (Phone50 as IUIFieldBindingDataBindControl).ReadOnly = flag;
            (Address89 as IUIFieldBindingDataBindControl).ReadOnly = flag;

            (IsZT68 as IUIFieldBindingDataBindControl).ReadOnly = flag;
            (GeneralAgency54 as IUIFieldBindingDataBindControl).ReadOnly = flag;
            (Agency50 as IUIFieldBindingDataBindControl).ReadOnly = flag;
            (TotalNo40 as IUIFieldBindingDataBindControl).ReadOnly = flag;

            UFGrid dataGrid = this.DataGrid5 as UFGrid;
            DataTable dataSource = dataGrid.DataSource as DataTable;
            if (dataSource == null) return;
            if (dataSource.Rows.Count == 0) return;
            for (int i = 0; i < dataSource.Rows.Count; i++)
            {
                dataGrid.SetColEnable("TBFee", !flag);
                dataGrid.SetColEnable("ZMDProfit", !flag);
                dataGrid.SetColEnable("ZDLProfit", !flag);
                dataGrid.SetColEnable("GSTransportFee", !flag);
                dataGrid.SetColEnable("GSProfit", !flag);
                dataGrid.SetColEnable("DevideRatio", !flag);
            }
        }

        /// <summary>
        /// 设置淘宝相关属性只读状态
        /// </summary>
        /// <param name="flag"></param>
        private void SetTBFieldReadOnly(bool flag)
        {
            (Org0 as IUIFieldBindingDataBindControl).ReadOnly = flag;
            (DocType1 as IUIFieldBindingDataBindControl).ReadOnly = flag;
            (DocNo0 as IUIFieldBindingDataBindControl).ReadOnly = flag;
            (Customer0 as IUIFieldBindingDataBindControl).ReadOnly = flag;

            (DocDate0 as IUIFieldBindingDataBindControl).ReadOnly = flag;
            (SaleWH0 as IUIFieldBindingDataBindControl).ReadOnly = flag;
            (SaleType92 as IUIFieldBindingDataBindControl).ReadOnly = flag;
            (Credit44 as IUIFieldBindingDataBindControl).ReadOnly = flag;

            (CreditNoCheck80 as IUIFieldBindingDataBindControl).ReadOnly = flag;
            (IsZY17 as IUIFieldBindingDataBindControl).ReadOnly = flag;
            (ZYCustomer53 as IUIFieldBindingDataBindControl).ReadOnly = flag;
            (DocState0 as IUIFieldBindingDataBindControl).ReadOnly = flag;

            (Memo116 as IUIFieldBindingDataBindControl).ReadOnly = flag;

            UFGrid dataGrid = this.DataGrid5 as UFGrid;
            DataTable dataSource = dataGrid.DataSource as DataTable;
            if (dataSource == null) return;
            if (dataSource.Rows.Count == 0) return;
            for (int i = 0; i < dataSource.Rows.Count; i++)
            {
                dataGrid.SetColEnable("DocLineNo", !flag);
                dataGrid.SetColEnable("Item", !flag);
                dataGrid.SetColEnable("Qty", !flag);
                dataGrid.SetColEnable("WH", !flag);
                dataGrid.SetColEnable("FinalAmount", !flag);
            }
        }

        /// <summary>
        /// 设置界面默认值
        /// </summary>
        private void SetDefaultValue()
        {
            //设置组织
            if (this.Model.SHFastShip.FocusedRecord.Org == null)
            {
                this.Model.SHFastShip.FocusedRecord.Org = PDContext.Current.OrgRef.ID;
                this.Model.SHFastShip.FocusedRecord.Org_Code = PDContext.Current.OrgRef.CodeColumn;
                this.Model.SHFastShip.FocusedRecord.Org_Name = PDContext.Current.OrgRef.NameColumn;
            }

            //设置单据日期
            if (this.Model.SHFastShip.FocusedRecord.DocDate == DateTime.MinValue)
            {
                this.Model.SHFastShip.FocusedRecord.DocDate = PDContext.Current.LoginDate;
            }

            //设置单据状态
            if (this.Model.SHFastShip.FocusedRecord.DocState == null)
            {
                this.Model.SHFastShip.FocusedRecord.DocState = 0;
            }
        }

        /// <summary>
        /// 设置控件及按钮状态
        /// </summary>
        private void SetControlsStatus()
        {
            //设置按钮状态
            SetBtnStatus();

            //依据状态设置界面是否可编辑
            SetUIEditable();

            //设置表格是否可编辑
            SetGridEditable();

            //设置表格颜色
            SetGridColor();
        }

        /// <summary>
        /// 设置按钮状态
        /// </summary>
        private void SetBtnStatus()
        {
            //隐藏工具条按钮
            BtnOk.Visible = false;
            BtnClose.Visible = false;
            BtnSubmit.Visible = false;
            BtnAttachment.Visible = false;
            BtnFlow.Visible = false;
            ReLoad.Visible = false;
            OnPrintSHBill.Visible = false;
            OnPrintSHShip.Visible = false;

            //审核后,抛单按钮可用
            if (this.Model.SHFastShip.FocusedRecord != null && (this.Model.SHFastShip.FocusedRecord.ShipDoc ?? 0) > 0)
            {
                if (this.Model.SHFastShip.FocusedRecord.Customer_DescFlexField_PrivateDescSeg8 == "True")
                {
                    //下线客户
                    OnShipToRCV.Enabled = false;
                    OnShipToSO.Enabled = false;
                }
                else
                {
                    //非下线客户且已审核,才能进行抛单
                    OnShipToRCV.Enabled = true;
                    OnShipToSO.Enabled = true;
                }
            }
            else
            {
                OnShipToRCV.Enabled = false;
                OnShipToSO.Enabled = false;
            }

            //按钮初始状态
            InitToolBarStatus();
            OnSOList.Enabled = false;
            OnLoadAvailableQty.Enabled = false;
            Menu1.Enabled = false;
            Menu6.Enabled = false;
            OnReserve.Enabled = false;
            Menu7.Enabled = false;

            //依据状态控制工具条按钮
            if (this.Model.SHFastShip.FocusedRecord == null || this.Model.SHFastShip.FocusedRecord.ID <= 0)
            {
                //新增状态
                this.BtnApprove.Enabled = false;
                this.BtnUndoApprove.Enabled = false;
                this.BtnDelete.Enabled = false;

                OnSOList.Enabled = true;
            }
            else if (this.Model.SHFastShip.FocusedRecord.DocState == 0)
            {
                //开立状态
                this.BtnUndoApprove.Enabled = false;

                OnSOList.Enabled = true;
                OnLoadAvailableQty.Enabled = true;

                //整单关闭
                Menu1.Enabled = true;

                //预留按钮
                if (ShipDoc.Reserved==true)
                {
                    Menu7.Enabled = true;
                }
                else
                {
                    OnReserve.Enabled = true;
                }
            }
            else if (this.Model.SHFastShip.FocusedRecord.DocState == 1)
            {
                //已核准
                this.BtnDelete.Enabled = false;
                this.BtnApprove.Enabled = false;

            }
            else if (this.Model.SHFastShip.FocusedRecord.DocState == 2)
            {
                //挂起
                this.BtnDelete.Enabled = false;
                this.BtnApprove.Enabled = false;

            }
            else if (this.Model.SHFastShip.FocusedRecord.DocState == 3)
            {
                //关闭
                this.BtnDelete.Enabled = false;
                this.BtnApprove.Enabled = false;

                //整单打开
                Menu6.Enabled = true;
            }
        }

        /// <summary>
        /// 设置表格颜色
        /// </summary>
        private void SetGridColor()
        {
            UFGrid dataGrid = this.DataGrid5 as UFGrid;
            DataTable dataSource = dataGrid.DataSource as DataTable;
            if (dataSource == null) return;
            if (dataSource.Rows.Count == 0) return;


            for (int i = 0; i < dataSource.Rows.Count; i++)
            {
                //淘宝发货,发货金额小于成本金额，标绿
                if (this.Model.SHFastShip.FocusedRecord != null
                    && this.Model.SHFastShip.FocusedRecord.DocType_Code == "U914")
                {
                    decimal amount = PubTool.ToDecimal(dataSource.Rows[i]["Amount"].ToString());
                    decimal finalamount = PubTool.ToDecimal(dataSource.Rows[i]["FinalAmount"].ToString());
                    if (finalamount < amount)
                        dataGrid.SetRowStyle(i, "", "#008000");
                }

                //所有发货，可用量不足的行，标红
                decimal aqty = PubTool.ToDecimal(dataSource.Rows[i]["AvailableQty"].ToString());
                decimal qty = PubTool.ToDecimal(dataSource.Rows[i]["Qty"].ToString());
                int state = PubTool.ToInt(dataSource.Rows[i]["DocLineState"].ToString());
                if (ShipDoc.Reserved==false && qty > aqty)
                {
                    dataGrid.SetRowStyle(i, "", "#FF0000");
                }
                //所有发货，数量为0或挂起、关闭，标灰
                if (qty == 0 || state == 2 || state == 3)
                {
                    dataGrid.SetRowStyle(i, "", "#999999");
                }
            }
        }

        /// <summary>
        /// 设置表格是否可编辑
        /// </summary>
        private void SetGridEditable()
        {
            UFGrid dataGrid = this.DataGrid5 as UFGrid;
            DataTable dataSource = dataGrid.DataSource as DataTable;
            if (dataSource == null) return;
            if (dataSource.Rows.Count == 0) return;

            //设置料品是否可录入
            for (int i = 0; i < dataSource.Rows.Count; i++)
            {
                //if(!string.IsNullOrEmpty( dataSource.Rows[i]["SODocNo"].ToString()))
                //    dataGrid.SetCellDisabled(i, "Item");

                if (!string.IsNullOrEmpty(dataSource.Rows[i]["Item"].ToString()))
                    dataGrid.SetCellDisabled(i, "Item");
            }
        }

        private void InitToolBarStatus()
        {
            this.BtnSave.Enabled = true;
            this.BtnCancel.Enabled = true;
            this.BtnAdd.Enabled = true;
            this.BtnDelete.Enabled = true;
            //this.BtnCopy.Enabled = true;
            //this.BtnSubmit.Enabled = true;
            this.BtnApprove.Enabled = true;
            this.BtnUndoApprove.Enabled = true;
            this.BtnFind.Enabled = true;
            this.BtnList.Enabled = true;
            this.BtnFirstPage.Enabled = true;
            this.BtnPrevPage.Enabled = true;
            this.BtnNextPage.Enabled = true;
            this.BtnLastPage.Enabled = true;
            //this.BtnAttachment.Enabled = true;
            //this.BtnFlow.Enabled = true;
            this.BtnOutput.Enabled = true;
            this.BtnPrint.Enabled = true;
        }


        #endregion

        #endregion

    }
}